package com.zb.manager.controller;

import com.zb.model.TGoods;
import com.zb.service.ManagerService;
import com.zb.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/goods")
public class GoodsControllers {
	
	@Autowired
	private ManagerService ms;
	
	@Autowired
	private CategoryServiceImpl cs;
	
	@GetMapping("/goods_list")
	public String goods_list(HttpSession session) throws Exception{
		List<TGoods> goodsList= ms.selectAllGoodsInDB();
		session.setAttribute("goodsList", goodsList);
		return "admin/goodslist";
	}
	
	@GetMapping("/goods_stock_list")
	public String goods_stock_list(HttpSession session) throws Exception{
		List<TGoods> goodsList= ms.selectAllGoodsInDB();
		session.setAttribute("goodsList", goodsList);
		return "admin/stocklist";
	}

	@RequestMapping("/addGood")
	public String addGood(ServletRequest request,
						  TGoods goods,
						  @RequestParam("headerimg") MultipartFile file)throws Exception{

		String url = request.getServletContext().getRealPath("/")+"static/images/goods/";

		//把文件长传到tomcat服务器
		file.transferTo(new File(url+file.getOriginalFilename()));

		//进行商品路径的拼接
		String uuid=UUID.randomUUID().toString();
		goods.setGoodsId(uuid);
		goods.setGoodsImg("static/images/goods/"+file.getOriginalFilename());

		//添加创建时间和修改时间
		Date date=new Date();
		goods.setCreateTime(date);
		goods.setUpdateTime(date);
		System.out.println("添加的商品信息为："+goods);
		ms.addGoods(goods);
		JOptionPane.showMessageDialog(null, "ADD OK");
		return "admin/goodslist";
	}


	@RequestMapping("/reGoods")
	public String reGoods(TGoods goods)throws Exception{

        Date date=new Date();
        goods.setUpdateTime(date);
		System.out.println("将要更改的书目为："+goods);
		ms.reGoods(goods);
		JOptionPane.showMessageDialog(null, "MODIFY OK");
		return "redirect:/goods/goods_list";
	}
	
	@RequestMapping("/reStock")
	public String reStock(HttpServletRequest request,HttpServletResponse response)throws Exception{
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
		String goodsId=request.getParameter("goodsId");
		int goodsStock=Integer.parseInt(request.getParameter("goodsStock"));
		TGoods goods=new TGoods();
		goods.setGoodsId(goodsId);
		goods.setGoodsStock(goodsStock);
		ms.reStock(goods);
		JOptionPane.showMessageDialog(null, "MODIFY OK");
		return "admin/ok";
	}
	
	@RequestMapping("/DimSearch")
	public String DimSearch(String goods_dimSearch,HttpSession session) {
		List<TGoods> list=cs.dimSelectService(goods_dimSearch);
		session.setAttribute("mdimgoods", list);
		return"admin/dimgoods";
	}
	
	@RequestMapping("/deleteGoods")
	public String deleteGoods(HttpServletRequest request)throws Exception{
		String goodsid=request.getParameter("goodsid");
		ms.deleteGoods(goodsid);
		return "admin/goodslist";
	}
	
}
